package v6

import (
	"encoding/json"
	"fmt"
	"mindonmap/pkgs/setting"
)

type PrivilegeController struct {
	// 新增字段
	CloudStorageLimit map[string]int      // 云存储限制, key为identity, value为数量
	MindNodeLimit     map[string]int      // 脑图节点限制
	ExportFormats     map[string][]string // 允许导出格式
	Watermark         map[string]bool     // 导出图片水印配置
	VersionLimit      map[string]int      // 历史版本限制
	PrivilegeConfig   PrivilegeConfig
}
type PrivilegeConfig struct {
	CloudStorageLimit map[string]int
	MindNodeLimit     map[string]int
	ExportFormats     map[string][]string
	Watermark         map[string]bool
	VersionLimit      map[string]int
}

func init() {
	//pc := NewPrivilegeController()
	//fmt.Println(pc.CheckPrivilege("free", "export_formats", 0,"svg"))
}

// NewPrivilegeController 初始化
func NewPrivilegeController() *PrivilegeController {
	fmt.Println("NewPrivilegeController 初始化")
	// 从 PrivilegeYaml 中获取所需字段
	cloudStorageLimit := make(map[string]int)
	mindNodeLimit := make(map[string]int)
	exportFormats := make(map[string][]string)
	watermark := make(map[string]bool)
	versionLimit := make(map[string]int) // 历史版本限制

	for items, entry := range setting.PrivilegeYaml {
		switch items {
		case "cloud_storage_limit":
			for k, v := range entry.(map[interface{}]interface{}) {
				cloudStorageLimit[k.(string)] = v.(int)
			}
		case "mind_node_limit":
			for k, v := range entry.(map[interface{}]interface{}) {
				mindNodeLimit[k.(string)] = v.(int)
			}
		case "export_formats":
			for k, v := range entry.(map[interface{}]interface{}) {
				//exportFormats[k.(string)] = v.([]string)

				var value []string
				for _, format := range v.([]interface{}) {
					if item, ok := format.(string); ok {
						value = append(value, item)
					}
				}
				exportFormats[k.(string)] = value
			}
		case "watermark":
			for k, v := range entry.(map[interface{}]interface{}) {
				watermark[k.(string)] = v.(bool)
			}
		}
	}
	config := PrivilegeConfig{
		CloudStorageLimit: cloudStorageLimit,
		MindNodeLimit:     mindNodeLimit,
		ExportFormats:     exportFormats,
		Watermark:         watermark,
		VersionLimit:      versionLimit,
	}

	return &PrivilegeController{
		CloudStorageLimit: cloudStorageLimit,
		MindNodeLimit:     mindNodeLimit,
		ExportFormats:     exportFormats,
		Watermark:         watermark,
		VersionLimit:      versionLimit,
		PrivilegeConfig:   config,
	}
}

// CheckPrivilege 扩展后的权限校验
func (pc *PrivilegeController) CheckPrivilege(identity string, resource string, count int, format ...string) bool {

	var formats []string
	if len(format) > 0 {
		formats = append(formats, format...)
	}

	// 根据资源类型判断是否需要校验 format
	needCheckFormat := false
	if resource == "export_formats" {
		needCheckFormat = true
	}

	if needCheckFormat && len(formats) == 0 {
		return false // export 资源必须传入 format
	}

	// 云存储空间检查
	if resource == "cloud_storage_limit" {
		limit := 0 // 默认为 0,表示不限制
		switch identity {
		case "free":
			limit = pc.CloudStorageLimit["free"] // limit = 5
		case "vip":
			limit = pc.CloudStorageLimit["vip"] // limit = 0,保持默认
		}
		println(count, limit)
		if limit > 0 && count >= limit {
			return false
		} // 只对有限制的用户校验

	}

	// 脑图节点数量检查
	if resource == "mind_node_limit" {
		limit := pc.MindNodeLimit[identity]
		println("脑图节点数量检查", count, limit)
		if limit > 0 && count > limit {
			return false
		}
	}

	// 导出格式检查
	if needCheckFormat {
		if resource == "export_formats" {
			var validFormats []string
			switch identity {
			case "free":
				validFormats = pc.ExportFormats["free"]
			case "vip":
				validFormats = pc.ExportFormats["vip"] // 为空列表 [],跳过校验
			}
			if len(validFormats) > 0 && !stringInSlice(formats[0], validFormats) {
				return false
			}
		}
	}
	// 是否添加水印权限
	if resource == "watermark" {
		watermark := pc.Watermark[identity]
		if !watermark {
			return false
		}
	}

	// 历史版本数量检查
	if resource == "version_limit" {
		limit := pc.VersionLimit[identity]
		if limit > 0 && count >= limit {
			return false
		}
	}

	// 其他资源检查...

	return true
}

// stringInSlice 检查字符串是否在字符串切片中
func stringInSlice(str string, slice []string) bool {
	for _, s := range slice {
		if s == str {
			return true
		}
	}
	return false
}

func (pc *PrivilegeController) ExportConfig() string {
	config := pc.PrivilegeConfig // 获取内部的PrivilegeConfig实例
	data, _ := json.Marshal(config)
	return string(data)
}
